import pymongo
import requests
from pyquery import PyQuery as pq
url = "https://cn.bing.com/search?q=%s&first=%d&FORM=PERE"
client = pymongo.MongoClient("mongodb://localhost:27017")
db = client.Advanced
collection = db.Bing
def parse(keyword, page, track=False):
    global url
    res = requests.get(url % (keyword, (page-1) * 10 + 1))
    html = res.text
    doc = pq(html)
    answers = doc(".b_algo")
    for answer in answers.items():
        description = answer("p").text()
        title = answer(".b_title").text()
        answer_url = answer("a").attr("href")
        data = {
            "question" : keyword,
            "description" : description,
            "title" : title,
            "url" : answer_url,
        }
        collection.insert_one(data)
        if track:
            others = doc("ul")
            for other in others.items():
                if other.attr("class") == "b_vList b_divsec":
                    for li in other.items():
                        a = li("a")
                        for i in range(5):
                            parse(a.text(), i+1)
keyword = ["Windows", "Android", "Mac OS", "Python", "C++", "csdn", "ABC", "Go"]
for i in keyword:
    for j in range(10):
        parse(i, j+1)
    parse(i, 11, track=True)